package com.tanxi521.springboot.controller;

import com.tanxi521.springboot.bean.BanItemBean;
import com.tanxi521.springboot.bean.LimitItemBean;
import com.tanxi521.springboot.pojo.*;
import com.tanxi521.springboot.service.BanItemService;
import com.tanxi521.springboot.service.LimitItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: i-xiaotian
 * @Date: 2022/4/9 22:15
 * @EMAIL: 1254806967@qq.com
 */

@RestController
@RequestMapping("/item")
@CrossOrigin
@Slf4j
public class ItemLimitController {

    @Resource
    BanItemService banItemService;

    @Resource
    LimitItemService limitItemService;

    @Resource
    RedisTemplate redisTemplate;

    @GetMapping("/ban")
    @RestPoint
    public APIResult getBanItemList(Integer pageSize, Integer pageIndex) {
        List<BanItem> banItemList = banItemService.queryBanItemList(pageSize, pageIndex);
        int count = banItemService.queryBanItemCount();
        BanItemBean banItemBean = new BanItemBean();
        banItemBean.setBanItemList(banItemList);
        banItemBean.setPageCount(count / pageSize + 1);
        banItemBean.setCount(count);
        return new APIResult(banItemBean, APIResult.SUCCESS, "查询ban表成功");
    }

    @GetMapping("/limit")
    @RestPoint
    public APIResult getLimitItemList(Integer pageSize, Integer pageIndex) {
        List<LimitItem> limitItemList = limitItemService.queryLimitItemList(pageSize, pageIndex);
        int count = limitItemService.queryLimitItemCount();
        LimitItemBean limitItemBean = new LimitItemBean();
        limitItemBean.setLimitItemList(limitItemList);
        limitItemBean.setPageCount(count / pageSize + 1);
        limitItemBean.setCount(count);
        return new APIResult(limitItemBean, APIResult.SUCCESS, "查询限制表成功");
    }

    @PostMapping("/ban")
    @RestPoint
    public APIResult insertBanItem(@RequestBody BanItem banItem, HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        OaUser oa_user = (OaUser)redisTemplate.opsForValue().get(token);
        if (oa_user.getUserRole() != 1) {
            log.warn(oa_user.getUserName() + " 尝试无权限修改");
            return new APIResult(APIResult.FAIL, "你莫得权限");
        }
        int count = banItemService.insertBanItem(banItem);
        if (count != 0) {
            log.info(oa_user.getUserName() + " 添加禁用物品 " + banItem.getName());
            return new APIResult(APIResult.SUCCESS, "添加禁用物品成功");
        } else {
            log.info(oa_user.getUserName() + " 添加禁用物品失败");
            return new APIResult(APIResult.FAIL, "添加物品失败，请重试");
        }
    }

    @PostMapping("/limit")
    @RestPoint
    public APIResult insertLimitItem(@RequestBody LimitItem limitItem, HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        OaUser oa_user = (OaUser)redisTemplate.opsForValue().get(token);
        if (oa_user.getUserRole() != 1) {
            log.warn(oa_user.getUserName() + " 尝试无权限修改");
            return new APIResult(APIResult.FAIL, "你莫得权限");
        }
        int count = limitItemService.insertLimitItem(limitItem);
        if (count != 0) {
            log.info(oa_user.getUserName() + " 添加限制物品 " + limitItem.getItemName());
            return new APIResult(APIResult.SUCCESS, "添加限制物品成功");
        } else {
            return new APIResult(APIResult.FAIL, "添加限制失败，请重试");
        }
    }

    @DeleteMapping("/ban")
    @RestPoint
    public APIResult deleteBanItem(@RequestBody DeleteIds deleteIds, HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        OaUser oa_user = (OaUser)redisTemplate.opsForValue().get(token);
        if (oa_user.getUserRole() != 1) {
            log.warn(oa_user.getUserName() + " 尝试无权限修改");
            return new APIResult(APIResult.FAIL, "你莫得权限");
        }
        int count = banItemService.deleteBanItem(deleteIds.getIds());
        if (count != 0) {
            log.warn(oa_user.getUserName() + " 删除了一个禁用物品");
            return new APIResult(APIResult.SUCCESS, "删除禁用物品成功");
        } else {
            return new APIResult(APIResult.FAIL, "删除禁用物品失败，请重试");
        }
    }

    @DeleteMapping("/limit")
    @RestPoint
    public APIResult deleteLimitItem(@RequestBody DeleteIds deleteIds, HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        OaUser oa_user = (OaUser)redisTemplate.opsForValue().get(token);
        if (oa_user.getUserRole() != 1) {
            log.warn(oa_user.getUserName() + " 尝试无权限修改");
            return new APIResult(APIResult.FAIL, "你莫得权限");
        }
        int count = limitItemService.deleteLimitItem(deleteIds.getIds());
        if (count != 0) {
            log.warn(oa_user.getUserName() + " 删除了一个限制物品");
            return new APIResult(APIResult.SUCCESS, "删除限制物品成功");
        } else {
            return new APIResult(APIResult.FAIL, "删除限制物品失败，请重试");
        }
    }
}
